<?php
namespace Admin\Controller;
use Think\Controller;
class BookTypeController extends Controller {
	//图书类型列表
    public function index(){

		$type_list = D('BookType')->get_list();
		$this->assign('type_list',$type_list);
	//	dump($type_list);
		$this->display();
    }
	
	//添加图书类型
	public function add()
	{
		$submit = I('post.submit','');
		$type_name = I('post.type_name','');
		$keywords = I('post.keywords','');
		$description = I('post.description','');
		$status = I('post.status','1');
		$order = I('post.order','');
		$datetime = date('Y-m-d H:i:s',time());

		if(!empty($submit))
		{
			if(!empty($type_name ))
			{
				$data = array();
				$data['type_name'] = $type_name;
				$data['keywords'] = $keywords;
				$data['description'] = $description;
				$data['status'] = (int)$status;
				$data['order'] = (int)$order;
				$data['datetime'] = $datetime;
				$res = D('BookType')->add($data);
				if ($res['ok']) {
					$this->success('添加成功',__ROOT__.'/index.php/Admin/BookType/index');
				}else {
					$this->error('添加失败');
				}
			}else{
				$this->error('类型名称不能为空');
			}
			
		}
		//
		$this->display();
	}
	//修改图书类型
	public function edit()
	{
		$id = I('get.id','');
		if(empty($id))
		{
			$this->error('没有找到此书籍');
		}else
		{
			$submit = I('post.submit','');
			$type_name = I('post.type_name','');
			$keywords = I('post.keywords','');
			$description = I('post.description','');
			$status = I('post.status','1');
			$order = I('post.order','');
			$datetime = date('Y-m-d H:i:s',time());
			
			if (!empty($submit)) {
				$data = array();
				$data['type_name'] = $type_name;
				$data['keywords'] = $keywords;
				$data['description'] = $description;
				$data['status'] = (int)$status;
				$data['order'] = (int)$order;
				$data['datetime'] = $datetime;
				$res = D('BookType')->edit($id,$data);
				if ($res) {
					$this->success('修改成功',__ROOT__.'/index.php/Admin/BookType/index');
				}
			}else {
				//查找书
				$book = D('BookType')->find($id);
				$this->assign('type',$book[$id]);
				$this->assign('type_id',$id);
				$this->display();
			}
		}
	}
	//删除图书
	public function del()
	{
		$id = I('get.id','');
		if(empty($id))
		{
			$this->error('没有找到此书籍类型');
		}else
		{
			$res = D('BookType')->del($id);
			if ($res) {
				$this->success('删除成功');
			}else {
				$this->error('删除失败');
			}
		}
	}
	
}